package tests.read;

import static org.junit.Assert.fail;

import java.util.ArrayList;

import model.Budget;
import model.BudgetFactory;
import model.Line;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

import com.mongodb.BasicDBObject;
import com.mongodb.util.JSON;

/*
 * 
 * Attempts to fetch a line in a budget with an ID of -100
 * [Should be a false postive test]
 * 
 */

public class LineQueryTest00
{
	Budget budget;
	BudgetFactory factory;
	Line line;

	@Before
	public void setUp() throws Exception
	{
		 factory = new BudgetFactory();
		 budget = (Budget) factory.create();
         budget.setName("Test budget");
         budget.setDescription("This is a test budget.");
         
         line = (Line) budget.createLine();
		 budget.add(line);
		 budget.commit();
	}

	@After
	public void tearDown() throws Exception
	{
		Helper.delete( line, "Lines", WorkletContext.getInstance());
		Helper.delete( budget, "Budgets", WorkletContext.getInstance());
	}

	@Test
	public void test()
	{
		try
		{
			BasicDBObject criteria = new BasicDBObject("entry.id", -100);
			String predicate = JSON.serialize(criteria);
			
			@SuppressWarnings("unchecked")
			ArrayList<Budget> lines = Helper.query("Lines", predicate, WorkletContext.getInstance());
			
			if(lines.size() != 0)
			{
				fail("Fetching line with invalid Id found results");
			}
		}
		catch (Exception e)
		{
			fail("Fetching line by false id failed with exception " + e);
		}
	}
}